import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:photo_frame/main.dart';
import 'package:photo_frame/pages/benediction/benediction_page.dart';
import 'package:photo_frame/pages/calendar/calendar_page.dart';
import 'package:photo_frame/pages/me/me_page.dart';
import 'package:photo_frame/provider/frames_provider.dart';
import 'package:photo_frame/provider/schedule_provider.dart';
import 'package:photo_frame/router/router_mapping.dart';
import 'package:photo_frame/user/user_manager.dart';
import 'package:photo_frame/utils/a_logger.dart';
import 'package:photo_frame/utils/calendar_helper.dart';
import 'package:photo_frame/utils/localization_util.dart';
import 'package:provider/provider.dart';

class TabbarPage extends StatefulWidget {
  @override
  State<TabbarPage> createState() => _TabbarState();

  TabbarPage({super.key});
}

class _TabbarState extends State<TabbarPage> with TickerProviderStateMixin {
  late PageController _tb;
  TabBarPageVideModel vm = TabBarPageVideModel();

  @override
  void initState() {
    super.initState();
    _tb = PageController();
    CalendarHelper.instance.retrieveCalendars();
    UserManager.instance.syncUserInfo();
  }

  @override
  void dispose() {
    _tb.dispose();
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    ALogger.e("didChangeDependencies init-_framesViewModel.frames?: ${context.read<FramesViewModel>().hashCode}");

    context.read<FramesViewModel>().fetch();

  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) => vm,
      child: Scaffold(
        body: PageView(
          controller: _tb,
          physics: NeverScrollableScrollPhysics(),
          children: [
            getPageBuild("/gallery").call(null),
            getPageBuild("/calendar").call(null),
            getPageBuild("/benediction").call(null),
            getPageBuild("/deviceList").call(null),
            MePage()
          ],
        ),
        bottomNavigationBar: Theme(
          data: ThemeData(
            // 去掉水波纹效果
            splashColor: Colors.transparent,
          ),
          child: Selector<TabBarPageVideModel, int>(
            selector: (_, vm) => vm.currentIndex,
            shouldRebuild: (n1, n2) => n1 != n2,
            builder: (BuildContext context, int value, Widget? child) {
              return BottomNavigationBar(
                currentIndex: value,
                backgroundColor: Colors.white,
                selectedFontSize: 10,
                unselectedFontSize: 10,
                selectedItemColor: Color(0xff3f74ef),
                unselectedItemColor: Color(0xff323333),
                type: BottomNavigationBarType.fixed,
                selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
                unselectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
                onTap: (idx) {
                  _tb.jumpToPage(idx);
                  vm.setCurrentIndex(idx);
                },
                items: [
                  // 导航子项
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      "images/icon_gallery.png",
                      width: 30,
                      height: 30,
                    ),
                    activeIcon: Image.asset(
                      "images/icon_gallery_light.png",
                      width: 30,
                      height: 30,
                    ),
                    label: appLocalForContext(context).gallery,
                  ),
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      "images/icon_schedule.png",
                      width: 30,
                      height: 30,
                    ),
                    activeIcon: Image.asset(
                      "images/icon_schedule_light.png",
                      width: 30,
                      height: 30,
                    ),
                    label: appLocalForContext(context).schedule,
                  ),
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      "images/icon_bless.png",
                      width: 30,
                      height: 30,
                    ),
                    activeIcon: Image.asset(
                      "images/icon_bless_light.png",
                      width: 30,
                      height: 30,
                    ),
                    label: appLocalForContext(context).blessing,
                  ),
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      "images/icon_frame.png",
                      width: 30,
                      height: 30,
                    ),
                    activeIcon: Image.asset(
                      "images/icon_frame_light.png",
                      width: 30,
                      height: 30,
                    ),
                    label: appLocalForContext(context).frame,
                  ),
                  BottomNavigationBarItem(
                    icon: Image.asset(
                      "images/icon_me.png",
                      width: 30,
                      height: 30,
                    ),
                    activeIcon: Image.asset(
                      "images/icon_me_light.png",
                      width: 30,
                      height: 30,
                    ),
                    label: appLocalForContext(context).me,
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}

class TabBarPageVideModel extends ChangeNotifier {
  int currentIndex = 0;

  setCurrentIndex(int index) {
    currentIndex = index;
    notifyListeners();
  }
}
